package com.ywjt.pinkelephant.dynamic;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.stx.xhb.xbanner.XBanner;
import com.ywjt.pinkelephant.R;
import com.ywjt.pinkelephant.base.BaseVPFragment;
import com.ywjt.pinkelephant.dynamic.activity.ActivityDistributeDetail;
import com.ywjt.pinkelephant.dynamic.activity.ActivityJiangMore;
import com.ywjt.pinkelephant.dynamic.activity.ActivityLiveMore;
import com.ywjt.pinkelephant.dynamic.activity.ActivityPodcastDetail;
import com.ywjt.pinkelephant.dynamic.adapter.DynamicHomeAdapter;
import com.ywjt.pinkelephant.dynamic.adapter.DynamicJiangAdapter;
import com.ywjt.pinkelephant.dynamic.adapter.DynamicLiveAdapter;

import java.util.ArrayList;
import java.util.List;


public class DynamicIndex extends BaseVPFragment implements View.OnClickListener {
    private Context context;
    private View view;
    private XBanner banner;
    private List<Integer> banners;
    private ArrayList<String> data;
    private RecyclerView rcCommon;
    private DynamicHomeAdapter homeAdapter;

    @Override
    public void onClick(View v) {
        switch (v.getId()){
//            case R.id.tvJiangMore:
//                ActivityJiangMore.actionStart(context);
//                break;
//            case R.id.tvLiveMore:
//                ActivityLiveMore.actionStart(context);
//                break;
        }
    }

    @Override
    protected void onFragmentVisibleChange(boolean isVisible) { }

    @Override
    protected void onFragmentFirstVisible() { }

    public static DynamicIndex newInstance(String content) {
        Bundle arguments = new Bundle();
        arguments.putString("string", content);
        DynamicIndex fl = new DynamicIndex();
        fl.setArguments(arguments);
        return fl;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.dynamic_index, container, false);
            context = getActivity();
            initView();
        }
        return view;
    }

    public void initView(){
        banner=view.findViewById(R.id.banner);
        rcCommon=view.findViewById(R.id.rcCommon);
        banners=new ArrayList<>();
        data=new ArrayList<>();

        banners.add(R.mipmap.icon_banner);
        banners.add(R.mipmap.icon_banner);
        banners.add(R.mipmap.icon_banner);
        banners.add(R.mipmap.icon_banner);
        banner.setData(R.layout.item_banner1,banners,null);
        banner.loadImage(new XBanner.XBannerAdapter() {
            @Override
            public void loadBanner(XBanner banner, Object object, View view, int position) {
                RelativeLayout container=(RelativeLayout) view;
                ImageView ivBanner=container.findViewById(R.id.ivBanner);
                Glide.with(context).load(banners.get(position))
                         .apply(new RequestOptions().transforms(new CenterCrop(), new RoundedCorners(10)))
                        .into(ivBanner);
            }
        });

        data.add("");
        data.add("");
        data.add("");
        data.add("");

        homeAdapter=new DynamicHomeAdapter(data,context);
        GridLayoutManager layoutManager = new GridLayoutManager(context,2,RecyclerView.VERTICAL,false) {
            @Override
            public boolean canScrollVertically() {
                // 直接禁止垂直滑动
                return false;
            }
        };
        rcCommon.setAdapter(homeAdapter);
        rcCommon.setLayoutManager(layoutManager);

        homeAdapter.setClickListener(new DynamicHomeAdapter.OnItemClickListener() {
            @Override
            public void onClick(View view, int position) {
                switch (position){
                    case 0:
                        ActivityJiangMore.actionStart(context);
                        break;
                    case 1:
                        ActivityLiveMore.actionStart(context);
                        break;
                    case 3:
                        ActivityPodcastDetail.actionStart(context);
                        break;
                    case 2:
                        ActivityDistributeDetail.actionStart(context);
                        break;
                }
            }

            @Override
            public void onSelected(View view, int position) {

            }
        });
    }
}